var Connect = YAHOO.util.Connect,
        Dom = YAHOO.util.Dom,
        Json = YAHOO.lang.JSON;

var productController = {
    
    setTextToHiddenInput: function(selectElementId, inputHiddenId) {
        var text = this.getSelectText(selectElementId);
        document.getElementById(inputHiddenId).value = text;
    },
    getSelectText: function(inputName) {
        var elements = document.getElementsByName(inputName);
        var element;
        var text = '';
        // Fix for firefox
        if (elements.length == 0) {
            element = document.getElementById(inputName);
        }
        else {
            element = elements[0];
        }

        for (var j = 0; j < element.length; j++) {

            if (element.options[j].selected == true) {
                text = element.options[j].text;
                break;
            }
        }

        return text;
    },
    asyncRequestFalse: function() {
        alert('AsyncRequest False');
    },
    getListProduct: function(url) {
        var height = Dom.getDocumentHeight();
        Dom.removeClass('overlay', 'hide');
        Dom.setStyle('overlay', 'height', height+'px');
        Dom.addClass('overlay', 'show');
        
        var strUrl = serverUrl + url;
        Connect.asyncRequest('POST', strUrl, {
            success: this.getListProductSuccess,
            failure: this.asyncRequestFalse
        });
    },
    getListProductSuccess: function(xmlhttp) {
        var strData = xmlhttp.responseText;
//        var aryData = Json.parse(strData);
        Dom.removeClass('overlay', 'show');
        Dom.addClass('overlay', 'hide');
        Dom.get('content').innerHTML = strData;
    },
    
    searchProduct:function(){
        var keyword = Dom.get('keyword').value;
        if(keyword == ''){
            alert('Nhập từ khóa tìm kiếm');
            return false;
        }
        var strUrl = serverUrl+'/product/tim-kiem/'+keyword;
        window.location.href = strUrl;
    }
};